﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace eShop.Model
{
    public class OrderItemCollection : List<OrderItem>
    {
        public double TotalPrice
        {
            get
            {
                double totalPrice = 0;
                this.ForEach(orderItem => totalPrice += orderItem.TotalPrice);
                return totalPrice;
            }
        }

        public double TotalWeight
        {
            get
            {
                double totalWeight = 0;
                this.ForEach(orderItem => totalWeight += orderItem.TotalWeight);
                return totalWeight;
            }
        }

        public void AddItem(OrderItem item)
        {
            OrderItem itemExist = this.Find(item.ProductId, item.StockId);
            if (itemExist != null)
            {
                itemExist.Quantity += item.Quantity;
            }
            else
            {
                base.Add(item);
            }
        }

        public OrderItem Find(int productId, int stockId)
        {
            return this.Find(item => item.ProductId == productId && item.StockId == stockId);
        }

        public bool Exists(int productId, int stockId)
        {
            return this.Exists(item => item.ProductId == productId && item.StockId == stockId);
        }

        public void SetOrderId(int orderId)
        {
            this.ForEach(orderItem => orderItem.OrderId = orderId);
        }
    }
}
